'use strict';
var QoSModalDemo = angular.module('FusionStorUI.QoS', []);
QoSModalDemo.controller('QoSDemoCtrl',['$scope','$modal', '$http', function($scope, $modal, $http) {
    var modalInstance;
    $scope.templateData = ''; 
    $scope.open = function(url) {  
        $scope.item = {};
        $http.get(url).success(function(data){
            $scope.templateData = data;
            modalInstance = $modal.open({  
                template : $scope.templateData,
                controller : QoSModalInstanceCtrl, 
                backdrop:'static',  
                keyboard: false,
                resolve : { 
                    item : function() {   
                        return $scope.item;  
                    }
                }
            });  
        });  
    };  
}]); 

var QoSModalInstanceCtrl = function($scope, $modalInstance, item) {  
    $scope.item = item;  
    $scope.selected = {  
        item : $scope.item 
    };  
    $scope.ok = function() {
        var qosname = $("#qos_create_modal").find("#qos_name_cr");
        var throt_iops_avg = $("#qos_create_modal").find("#throt_iops_avg");
        var throt_burst_iops_max = $("#qos_create_modal").find("#throt_burst_iops_max");
        var throt_burst_iops_time = $("#qos_create_modal").find("#throt_burst_iops_time");
        var throt_mbps_avg = $("#qos_create_modal").find("#throt_mbps_avg");
        var throt_burst_mbps_max = $("#qos_create_modal").find("#throt_burst_mbps_max");
        var throt_burst_mbps_time = $("#qos_create_modal").find("#throt_burst_mbps_time");
        var bValid = true;
        bValid = bValid && checkRegexp_num(qosname, /^[0-9a-z_-]{1,32}$/, "名称必须由1到32位以下小写英文字母、数字、中划线或下划线构成。");
        //      bValid = bValid && checkRegexp_num(throt_iops_avg, /^[0-9]+$/i, "IOPS平均值必须为大于0的整数");
        //      bValid = bValid && checkRegexp_num(throt_burst_iops_time, /^[0-9]+$/i, "IOPS峰值持续时间必须为大于0的整数");
        bValid = bValid && checkRegexp_num(throt_burst_iops_max, /^[0-9]+$/i, "IOPS峰值必须为大于0的整数。");
        bValid = bValid && checkRegexp_num(throt_burst_mbps_max, /^[0-9]+$/i, "MBPS峰值必须为大于0的整数。");
        //      bValid = bValid && checkRegexp_num(throt_mbps_avg, /^[0-9]+$/i, "MBPS平均值必须为大于0的整数");
        //      bValid = bValid && checkRegexp_num(throt_burst_mbps_time, /^[0-9]+$/i, "MBPS峰值持续时间必须为大于0的整数");

        if (bValid) {
            if (enable_submit) {
                enable_submit = false;
                var $submitBtn = $('.modal-footer').find('.btn-blue');
                $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
                $('.rtn-tip').slideUp('fast');
                $.post("/qos_create", {
                        'name': qosname.val(),
                        'throt_iops_avg': throt_iops_avg.val(),
                        'throt_burst_iops_max': throt_burst_iops_max.val(),
                        'throt_burst_iops_time': throt_burst_iops_time.val(),
                        'throt_mbps_avg': throt_mbps_avg.val(),
                        'throt_burst_mbps_max':parseInt(throt_burst_mbps_max.val()) * 1024 * 1024,
                        'throt_burst_mbps_time': throt_burst_mbps_time.val(),
                    },
                    function(data) {
                        if (data.reply.is_success) {
                            $modalInstance.close($scope.selected.item);  
                            enable_submit = true;
                            $('#qos_table').trigger("reloadGrid");
                        } else {
                            var error = data.reply.error;
                            $('.rtn-tip').text(error).slideDown('fast');
                            $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
                            enable_submit = true;
                        }
                    });
            }
        }
    };  
    $scope.update = function(qos_id) { 
        var enable_submit=true;
        var qosname = $("#qos_update_modal").find("#qos_name_cr");
        var throt_iops_avg = $("#qos_update_modal").find("#throt_iops_avg");
        var throt_burst_iops_max = $("#qos_update_modal").find("#throt_burst_iops_max");
        var throt_burst_iops_time = $("#qos_update_modal").find("#throt_burst_iops_time");
        var throt_mbps_avg = $("#qos_update_modal").find("#throt_mbps_avg");
        var throt_burst_mbps_max = $("#qos_update_modal").find("#throt_burst_mbps_max");
        var throt_burst_mbps_time = $("#qos_update_modal").find("#throt_burst_mbps_time");
        var bValid = true;
        bValid = bValid && checkRegexp(qosname, /^[0-9a-z_-]{1,32}$/, "名称必须由1到32位以下小写英文字母、数字、中划线或下划线构成。");
//      bValid = bValid && checkRegexp_num(throt_iops_avg, /^[0-9]+$/i, "IOPS平均值必须为大于0的整数");
//      bValid = bValid && checkRegexp_num(throt_burst_iops_time, /^[0-9]+$/i, "IOPS峰值持续时间必须为大于0的整数");
        bValid = bValid && checkRegexp_num(throt_burst_iops_max, /^[0-9]+$/i, "IOPS峰值必须为大于0的整数");
        bValid = bValid && checkRegexp_num(throt_burst_mbps_max, /^[0-9]+$/i, "MBPS峰值必须为大于0的整数");
//      bValid = bValid && checkRegexp_num(throt_mbps_avg, /^[0-9]+$/i, "MBPS平均值必须为大于0的整数");
//      bValid = bValid && checkRegexp_num(throt_burst_mbps_time, /^[0-9]+$/i, "MBPS峰值持续时间必须为大于0的整数");
      
        if (bValid) {
            if(enable_submit){     
                enable_submit=false;
                var $submitBtn = $('.modal-footer').find('.btn-blue');
                $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
                $('.rtn-tip').slideUp('fast');
                $.post("/qos_update",{
                    'id':qos_id,
                    'name':qosname.val(),
                    'throt_iops_avg':throt_iops_avg.val(),
                    'throt_burst_iops_max':throt_burst_iops_max.val(),
                    'throt_burst_iops_time':throt_burst_iops_time.val(),
                    'throt_mbps_avg':throt_mbps_avg.val(),
                    'throt_burst_mbps_max':parseInt(throt_burst_mbps_max.val()) * 1024 * 1024,
                    'throt_burst_mbps_time':throt_burst_mbps_time.val(),
                    },
                    function(data){
                        if (data.reply.is_success){
                            $('#qos_table').trigger("reloadGrid")
                            enable_submit=true;                              
                            $modalInstance.close($scope.selected.item);  
                        }else{
                            var error =  data.reply.error;
                             $('.rtn-tip').text(error).slideDown('fast');
                            $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
                            enable_submit=true;                           
                        }
                 });
           }
        }
    }

    $scope.cancel = function() {  
        $modalInstance.dismiss('cancel');  
    };  
};

